package com.xzp.bams.dao;

import com.xzp.bams.entity.Account;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @TODO
 * @Author xu
 * 2025/3/10
 */
public class FileDao implements IDao<Account, List<Account>>{
    File idFile = new File("id.txt");
    //存储所有Account账户信息至文件夹 account/86215xxx2025030001.txt
    File accFile = new File("account");
    public FileDao(){
        if(!idFile.exists()){
            try {
                idFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //若account文件夹不存在，则创建新文件夹
        if(!accFile.exists()){
            accFile.mkdirs();
        } //文件、或文件夹 不存在，先创建
    }

    //从id.txt文件中读取ID
    @Override
    public Long getId() {

        try(
                BufferedReader br = new BufferedReader(new FileReader(idFile));
        ){
            String s = br.readLine();
            //第一次读取文本，赋初始值
            if(s==null || s.equals("")){
                s = "100000000000000000";
            }
            Long id = Long.valueOf(s);
            return id;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    //从id.txt文件中写入ID
    @Override
    public void setId(Long id) {

        try (
                PrintWriter pw = new PrintWriter(idFile);
        ){
            pw.println(id);
            pw.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }


    //针对于Account账户信息  写  序列化
    public void writeAcc(Account acc){
        try (
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(accFile , acc.getId()+".txt")));
        ){
            oos.writeObject(acc);
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //针对于Account账户信息  读  反序列化
    public Account readAcc(Long id){
        try (
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(accFile , id+".txt")));
        ){
            Account acc = (Account)ois.readObject();
            return acc;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }


    //插入  写
    @Override
    public boolean insert(Account account) {
         writeAcc(account);
         return true;

    }

    //查询 读
    @Override
    public Account selectOne(Long id, String password) {
        Account account = this.selectOne(id);
        if(account!=null && account.getPassword().equals(password)){
            return account;
        }
        return null;

    }

    //查询 读
    @Override
    public Account selectOne(Long id) {
        Account account = readAcc(id);
        if(account!=null && account.getId().equals(id)){
            return account;
        }
        return null;

    }

    //查询所有 读
    @Override
    public List<Account> selectAll() {

        List<Account> list = new ArrayList<>();
        //获取文件夹下的所有文件名
        String[] strs = accFile.list();
        //id  86215xxx2025030001.txt
        for (String id : strs) {
            //id  86215xxx2025030001
            id = id.substring(0 , id.length()-4);
            //读数据
            Account account = readAcc(Long.valueOf(id));
            list.add(account);
        }
        return list;
    }


    //更新 写
    @Override
    public boolean update(Account account) {

        writeAcc(account);
        return true;
    }
}


